package com.yuncheng.oaApi.busService;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.entity.OaHysgl;
import com.yuncheng.oaApi.entity.OaHysxq;
import com.yuncheng.oaApi.service.IOaHysglService;
import com.yuncheng.oaApi.service.IOaHysxqService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component(value = "HysxqService")
public class HysxqService {

    @Resource
    @Lazy
    private IOaHysxqService oaHysxqService;


    @Resource
    @Lazy
    private IOaHysglService oaHysglService;

    //日期调用
    public static String getFetureDate(int lxts) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + lxts);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);

        return result;
    }
    //    循环日期
    public List<String> queryData(int lxts) {
        List<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i <= lxts; i++) {
            fetureDaysList.add(getFetureDate(i));
        }
        return fetureDaysList;
    }

    public List<List<String>> getHysList(int lxts){
        List<String> hysList = new ArrayList<>();
        //添加表头
        hysList.add("会议室");
        List<OaHysxq> hysmcList = oaHysxqService.list(new LambdaQueryWrapper<OaHysxq>().orderByAsc(OaHysxq::getFhysxh));
        //循环会议室名称
        if (CollectionUtils.isNotEmpty(hysmcList)){
            for (OaHysxq hys : hysmcList){
                hysList.add(hys.getFhysmc());
            }
        }
        //接收连续的天数
        List<String> lxtsList = queryData(lxts);
        //最终内容
        List<List<String>> zznr = new ArrayList<>();
        //添加循环出来的会议室列表
        zznr.add(hysList);
        //循环日期
        for (String rq :lxtsList){
            List<String> hysnrList = new ArrayList<>();
            hysnrList.add(rq);
            int index = 0;
            //循环会议室
            for (String hys : hysList ){
                if (index != 0){
                    List<OaHysgl> nrList = oaHysglService.list(new LambdaQueryWrapper<OaHysgl>()
                                            .eq(OaHysgl::getFlczt, OaCommonFlowConstant.FLOW_LCJS)
                                            .eq(OaHysgl::getFhydd, hys)
                                            .likeRight(OaHysgl::getFhykssj, rq));
                    if (CollectionUtils.isNotEmpty(nrList)){
                        String hy = "";
                        //循环添加内容
                        for (OaHysgl nr:nrList){
                            String hykssj = "";
                            String hyjssj = "";
                            if (nr.getFhykssj() != null){
                                hykssj = nr.getFhykssj().substring(11,16);
                            }
                            if (nr.getFhyjssj()  != null){
                                hyjssj = nr.getFhyjssj().substring(11,16);
                            }
                            if (StringUtils.isNotBlank(hy)){
                                hy += "\n";
                            }
                            hy += hykssj+"-"+hyjssj+"\n"+"("+ nr.getFhynr()+")";
                        }
                        hysnrList.add(hy);
                    }else {
                        hysnrList.add("");
                    }
                }
                index++;
            }
            zznr.add(hysnrList);
        }
        return zznr;


    }

}
